package com.alex.controller.AI_Writing_Assistant;

import com.alex.controller.System.AliClientToolNoStreamJson;
import com.alex.controller.System.entity.ChatEntity;
import com.alex.enumType.TypeEnum;
import com.alex.sevice.ChatGptService;
import com.alex.sevice.StaticService;

import common.beans.User;
import common.util.UserUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@SuppressWarnings("ALL")
@RequestMapping("/ai_writing_word")
@Tag(name = "AI 写作助手_孔柳茜")
@RestController
public class AI_Writing_Assistant_Controller {
    @Autowired
    private AliClientToolNoStreamJson aliClientToolNoStreamJson;


    @Resource
    StaticService staticService;
    @Autowired
    private ChatGptService chatGptService;

    @Operation(summary = "近似词", description = "接口以stream流式返回,参考各大 AI 聊天界面效果,返回结果为LIST")
    @GetMapping("/ai_writing_word")
    public void pubmed_search(@Parameter(name = "content", description = "要输入的内容", example = "cancer", required = true) String content,
                              HttpServletResponse response) throws IOException {
        System.out.println("用户提问的内容是:" + content);
        List<ChatEntity> list = new ArrayList<>();
        list.add(new ChatEntity("system", "任务描述：根据用户提供的关键词生成20个与之意思相近或相关的扩展词。这些词汇需要是英文检索词，并且每个英文词都需要附带中文翻译。 输出格式要求：请严格按照以下JSON格式返回结果，不要包含任何额外的信息。  [ {\"en\":\"example_word1\",\"cn\":\"示例词1\"}, {\"en\":\"example_word2\",\"cn\":\"示例词2\"}, ... {\"en\":\"example_word20\",\"cn\":\"示例词20\"} ]   具体步骤： 1. 分析用户输入词的含义及其相关领域。 2. 基于分析结果，找出20个与用户输入词意思相近或相关的英文词汇。 3. 对每一个找到的英文词汇提供准确的中文翻译。 4. 按照指定的JSON格式组织答案并返回。 注意事项： - 确保所有生成的词汇都与用户输入词紧密相关。 - 中文翻译应准确反映英文词汇的意思。 - 输出必须严格遵循给定的JSON结构，不添加额外的文字说明或其他内容。"));
        list.add(new ChatEntity("user", content));
        String send = aliClientToolNoStreamJson.send(list);
        staticService.insStat(content, "近似词");
        aliClientToolNoStreamJson.sendToClint(response, send);
    }

    @Operation(summary = "改写", description = "接口以stream流式返回,参考各大 AI 聊天界面效果,返回结果为LIST")
    @GetMapping("/rewrite")
    public void rewrite(@Parameter(name = "content", description = "要输入的内容", example = "cancer", required = true) String content, HttpServletResponse response) throws IOException {
        System.out.println("用户提问的内容是:" + content);
        List<ChatEntity> list = new ArrayList<>();
        list.add(new ChatEntity("system", "请根据用户提供的原始标题(请忽略掉标题中携带着的HTML标签样式,只关注具体文本)，创造五个新的标题版本。新标题需保持与原意一致，语种一致,但表达方式不同。最终结果请按照以下JSON格式返回：[\"标题A\",\"标题B\",\"标题C\",\"标题D\",\"标题E\"]。"));
        list.add(new ChatEntity("user", content));
        String send = aliClientToolNoStreamJson.send(list);
        staticService.insStat(content, "改写");
        aliClientToolNoStreamJson.sendToClint(response, send);
    }

    @Operation(summary = "英文学术润色", description = "")
    @GetMapping("/en_rs")
    public void en_rs(@Parameter(name = "content", description = "要输入的内容", example = "cancer", required = true) String content, HttpServletResponse response) throws IOException {
        staticService.insStat(content, "英文学术润色");
        try {
            User user = UserUtil.getUserUnloginAccess();
            chatGptService.ai_web_chat(response, user, false, TypeEnum.英文学术润色.getValue() + ",用户信息是:\n" + content, 0, 0);
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }

    @Operation(summary = "英译中", description = "")
    @GetMapping("/en_to_cn")
    public void en_to_cn(@Parameter(name = "content", description = "要输入的内容", example = "cancer", required = true) String content, HttpServletResponse response) throws IOException {
        staticService.insStat(content, "英译中");
        try {
            User user = UserUtil.getUserUnloginAccess();
            chatGptService.ai_web_chat(response, user, false, TypeEnum.英译中.getValue() + ",用户信息是:\n" + content, 0, 0);
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }

    @Operation(summary = "查找语法错误", description = "")
    @GetMapping("/check")
    public void check(@Parameter(name = "content", description = "要输入的内容", example = "cancer", required = true) String content, HttpServletResponse response) throws IOException {
        staticService.insStat(content, "查找语法错误");
        try {
            User user = UserUtil.getUserUnloginAccess();
            chatGptService.ai_web_chat(response, user, false, TypeEnum.查找语法错误.getValue() + ",用户信息是:\n" + content, 0, 0);
        } catch (Exception e) {
            e.printStackTrace(System.err);
        }
    }


}
